<template>
  <div class="login-container">
    <div class="video-container">
      <video
        src="@/assets/video.mp4"
        autoplay
        loop
        muted
      />
    </div>
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      auto-complete="off"
      label-position="left"
    >

      <div class="title-container">
        <img src="@/assets/logo/logo.png" alt="logo">
      </div>

      <el-form-item prop="account">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="username"
          v-model="loginForm.account"
          placeholder="Username"
          type="text"
        />
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="Password"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>

      <el-form-item prop="code">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="username"
          v-model="loginForm.code"
          placeholder="Code"
          type="text"
        >
          <template #suffix>
            <img v-if="url" :src="url" alt="url" @click="getCaptcha">
          </template>
        </el-input>
      </el-form-item>

      <el-button
        class="btn-login"
        :loading="loading"
        type="primary"
        style="width:100%;margin-bottom:30px;"
        @click.native.prevent="login"
      >Login</el-button>

      <div class="tips">
        仅用于IT培训教学使用，为保障您的个人信息安全，请勿向平台录入任何个人敏感信息 (如手机号、身份证号等)!
      </div>
    </el-form>
  </div>
</template>

<script>
import { getCaptchaAPI } from '@/api/user'
const key = '1234'
export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        account: 'demo',
        password: 'hm#qd@23!',
        code: ''
      },
      loginRules: {
        account: [
          { required: true, message: '请填写用户名', trigger: 'blur' },
          { min: 4, max: 14, message: '请填写 4-14 个字符长度', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请填写密码', trigger: 'blur' },
          { min: 9, max: 14, message: '请填写 9-14 个字符长度', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请填写验证码', trigger: 'blur' },
          { pattern: /^\d{1,2}$/, message: '请填写正确的验证码', trigger: 'blur' }
        ]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      url: ''
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    this.getCaptcha()
  },
  methods: {
    // 获取验证码
    async getCaptcha() {
      const blob = await getCaptchaAPI(key)
      this.url = URL.createObjectURL(blob)
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    // 登录
    async login() {
      try {
        // 整体校验
        await this.$refs.loginForm.validate()
        // 设置按钮的加载状态
        this.loading = true
        // 调用 user/loginAction 函数
        await this.$store.dispatch('user/loginAction', {
          ...this.loginForm,
          key
        })
        // 成功提示
        this.$message.success('登录成功')
        // 登录成功优先跳转至回跳地址，否则再去首页
        this.$router.replace(this.$route.query.redirectUrl || '/')
        // eslint-disable-next-line no-empty
      } catch (err) {
      } finally {
        // 关闭按钮的加载状态
        this.loading = false
      }
    }
  }
}
</script>

<style lang="scss">
$bg:#283443;
$light_gray:#fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: #333;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: #333;
      height: 47px;
      caret-color: #333;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px #fff inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid #ccc;
    border-radius: 5px;
    color: #454545;
    &::target {
      border-color:#e15536;
    }
  }
}
</style>

<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  display: flex;
  justify-content: center;
  align-items: flex-end;
  min-height: 100%;
  width: 100%;
  background-color: #fff;
  overflow: hidden;
  .video-container {
    position: absolute;
    left: 0;
    top: 0;
    width: 100vw;
    height: 100vh;
    video {
      width: 100vw;
      height: 100vh;
      object-fit: fill;
    }
  }

  .login-form {
    position: relative;
    width: 450px;
    max-width: 100%;
    padding: 30px 40px;
    margin: 0 auto;
    overflow: hidden;
    background: #fff;
    border-radius: 15px;
    .btn-login {
      height: 54px;
      background: #e15536;
      border-width: 0;
    }
    ::v-deep .el-input__suffix {
      img {
        position: relative;
        top: 4px;
        left: 20px;
      }
    }
  }

  .tips {
    font-size: 14px;
    color: red;
    line-height: 40px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;
    text-align: center;
    margin-bottom: 30px;
    img {
      height: 54px;
    }

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
